package ltd.newbee.mall.controller.admin;


import com.alipay.api.domain.StaffInfo;
import ltd.newbee.mall.entity.NewBeeMallGoods;
import ltd.newbee.mall.entity.NewbeeMallPurchaseInfo;
import ltd.newbee.mall.entity.NewbeeMallSupplierInfo;
import ltd.newbee.mall.exception.MarketException;
import ltd.newbee.mall.service.IPurchaseInfoService;
import ltd.newbee.mall.service.ISupplierInfoService;
import ltd.newbee.mall.service.impl.NewBeeMallGoodsServiceImpl;
import ltd.newbee.mall.util.KeyUtil;
import ltd.newbee.mall.util.PageQueryUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: purchasingAgentPurchaseInfoController
 * @Description: ${TODO}
 * @Author: Emily
 * @Date: 2021-09-06 10:02
 * @Version 1.0
 */
@Controller
@RequestMapping("/purchasingAgent/purchase")
public class PurchasingAgentPurchaseInfoController
{
    @Autowired
    private IPurchaseInfoService purchaseInfoService;

    // test
    @Autowired
    private ISupplierInfoService supplierInfoService;


    @Resource
    private NewBeeMallGoodsServiceImpl productService;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "5") Integer size,
                             Map<String, Object> map)
    {
        PageRequest pageRequest = PageRequest.of(page - 1, size);

        // 调用分页查询商品列表方法
        Page<NewbeeMallPurchaseInfo> purchseInfoPageList = purchaseInfoService.findAll(pageRequest);
        map.put("purchaseInfoPageList", purchseInfoPageList); // 设置商品分页列表
        map.put("currentPage", page);  // 设置当前页
        map.put("pageSize", size);

        return new ModelAndView("purchasingAgent/purchase/list", map);
    }

    /**
     * 展示卖家商品-新增和修改页面
     * 商品新增和 商品修改 公共一个页面
     * (区别 展示商品修改页面时要有商品ID)
     */
    @GetMapping("/index")
    public ModelAndView index(Map map)
    {
//        尝试
        List<NewbeeMallSupplierInfo> supplierInfoList = supplierInfoService.findAll();
        PageQueryUtil pageUtil = new PageQueryUtil(map);
        List<NewBeeMallGoods> productInfoList = (List<NewBeeMallGoods>) productService.getNewBeeMallGoodsPage(pageUtil);

        map.put("supplierInfoList",supplierInfoList);

        // TODO 待完善：最好能筛选出进货员
        map.put("productInfoList",productInfoList);


        return new ModelAndView("purchasingAgent/purchase/index", map);
    }

    /**
     * 商品新增/修改  功能
     */
    @PostMapping("/save")
    public ModelAndView save(NewbeeMallPurchaseInfo purchseInfo,
                             BindingResult bindingResult,
                             HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        if (bindingResult.hasErrors())
        {
            session.setAttribute("msg", bindingResult.getFieldError().getDefaultMessage());
            session.setAttribute("url", request.getContextPath() + "/purchasingAgent/purchase/index");
            return new ModelAndView("common/error");
        }

        try
        {
            NewbeeMallPurchaseInfo purchseInfoNew= new NewbeeMallPurchaseInfo();
        BeanUtils.copyProperties(purchseInfo, purchseInfoNew);
        purchseInfoNew.setPurchaseId(KeyUtil.genUniqueKey());

        NewBeeMallGoods productInfo = productService.getNewBeeMallGoodsById(purchseInfoNew.getGoodsId());
        purchseInfoNew.setGoodsName(productInfo.getGoodsName());
        purchaseInfoService.save(purchseInfoNew);   // 保存/更新 商品信息
        } catch (MarketException e)
        {
            session.setAttribute("msg", e.getMessage());
            session.setAttribute("url", request.getContextPath() + "/purchasingAgent/purchase/index");
            return new ModelAndView("common/error");
        }
        session.setAttribute("url", request.getContextPath() + "/purchasingAgent/purchase/list");
        return new ModelAndView("common/success");
    }


}
